<?php
	$departments = CJSON::encode(MDepartment::model()->findAll(array("condition"=>"is_active = TRUE","order"=>"id")));
		
?>

<script>
	//var departments = <?php echo $departments?>;
	var department_id = "<?php echo $department_id ?>";	
	
	function reloadPage(){
		/*		
		$("#tblData").jqGrid("setGridParam",{postData:{"department_id" :$("#department_id").val()},datatype:"json"}).trigger("reloadGrid");		
		$("#tblData").jqGrid("setGridParam",{page:8}).trigger("reloadGrid");		
		$("#idSrcTblData").val("");
		*/
		refreshGrid();
	}

	function refreshGrid(){		
		$("#tblData").jqGrid("setGridParam",{
			postData:{
				"department_id" :$("#department_id").val(),
				"tahun_ajaran_id" :$("#tahun_ajaran_id").val(),
			},
			datatype:"json"
		}).trigger("reloadGrid");
		$("#idSrcTblData").val("");
	}
	
	
	
	function printData(){
		var url="index.php?r=akMaster_TahunAjaran/print&department_id=" + $("#department_id").val();
		var winName = "Master Tahun Ajaran";
		var specs ="menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0";
		var width=800,height=500;
		var replace = false,onCenter = true;
		w = openWindow(winName,url,width,height,onCenter,specs,replace);
	}
	
	function closeTest(){
		
	}
	
	function viewRow(id){		
		var url="index.php?r=akademik/siswa/main/form&op=view&id=" + id;
		var winName = "";
		var specs ="menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0";
		var width=1100,height=800;
		var replace = true,onCenter = true;
		w = openWindow(winName,url,width,height,onCenter,specs,replace);
		var timer = setInterval(function() {   
			if(w.closed) {  
				clearInterval(timer);  
				reloadPage();
			}  
		}, 1000);
	}
	
	function activateRow(id){
		if (confirm('<?php echo Yii::t("dialog","Ubah status siswa menjadi aktif ?"); ?>')){			
			window.parent.showLoader();			
			var data = $("#tblData").jqGrid("getRowData",id);
			var editUrl = $("#tblData").jqGrid("getGridParam","editUrl");			
			$.ajax({	
				url : editUrl,
				type:"GET",
				async:true,
				data: {op: "activate",id:data.id},
				success: function(data) {
					if(data.actionStatus == 0){
						alert(data.actionMessage);
						$("#tblData").jqGrid("delRowData",id);	
					}else if(data.actionStatus == 1){
						alert(data.actionMessage);
					}						
				},
				complete:function(){
					window.parent.hideLoader();
				}
			});
		}
	}

	$(function(){
		var numberTemplate = {formatter: 'number', align: 'right', sorttype: 'number'};	 
		$("#tblData").jqGrid({
			url:'index.php?r=akademik/siswa/main/graduantedStudentSummaryList',
			editUrl:'index.php?r=akademik/penerimaan_murid/main/edit',
			postData:{"department_id":'<?php echo $department_id; ?>',"tahun_ajaran_id":<?php echo $tahun_ajaran_id;?>},
			datatype: 'local',
			loadonce:true,
			mtype:"GET",
			autowidth:true,
			height:"100%",
			altRows:true,
			ignoreCase:true,
			//$strFields="id,department_id,sid,name,if(gender='P','Pria','Wanita') as gender,birth_date,address,home_phone,handphone,email,ayah_nama,ibu_nama,wali_nama,is_active";
			colNames: ['id','Trial ID','SID','Tahun Ajaran', 'Nama',  'Jenis Kelamin', 'Tgl Lahir', 'Alamat','No Telp','Handphone','Alamat email','Ayah','Ibu','Wali','Status','Action'],
			colModel: [
				{name: 'id', index: 'id', width: 20,hidden:true,sorttype:'int',formatter:'int'},
				{name: 'trial_id', index: 'trial_id', width: 60,search:true},
				{name: 'sid', index: 'sid', width: 60,search:true},
				{name: 'tahun_ajaran', index: 'tahun_ajaran', width: 50,search:false,align:"center"},
				{name: 'name', index: 'name', width: 100,search:true},
				{name: 'gender', index: 'gender', width: 50,search:true},
				{name: 'birth_date', index: 'birth_date', width: 60, align: 'left', sorttype: 'date',
					formatter: 'date', formatoptions: {newformat: 'd-M-Y'}, datefmt: 'd-M-Y'},
				{name: 'address', index: 'address',width: 200},
				{name: 'home_phone', index: 'home_phone',width: 50},
				{name: 'handphone', index: 'handphone',width: 50},
				{name: 'email', index: 'email',width: 50},
				{name: 'ayah_nama', index: 'ayah_nama',width: 100,hidden:true},
				{name: 'ibu_nama', index: 'ibu_nama',width: 100,hidden:true},
				{name: 'wali_nama', index: 'wali_nama',width: 100,hidden:true},				
				{name: 'is_active', index: 'is_active', width: 30, align: 'center', formatter: 'checkbox'},
				{name: 'act', index: 'act', width: 60, align: 'center'},
				
			],
			gridview: true,
			rownumbers: false,
			rowNum: 10,
			rowList: [5, 10, 15],
			pager: '#navTblData',
			viewrecords: true,
			caption: 'Daftar Murid',
			jsonReader : { 
				root:"model",
				repeatitems: false			
			},
			loadComplete:function(data){
				console.log(data);
			},
			gridComplete: function(){ 
				var ids = jQuery("#tblData").getDataIDs(); 
				for(var i=0;i<ids.length;i++){ 
					var cl = ids[i]; 
					//btnEdit = "<input style='height:22px;width:20px;' type='button' value='E' onclick=jQuery('#rowed2').editRow("+cl+"); ></ids>"; 
					//btnDelete = "<input style='height:20px;width:20px;' type='button' value='S' onclick=jQuery('#rowed2').saveRow("+cl+"); />"; 
					btnEdit = "<img title='view' class='imgBtn' style='height:20px;width:20px;' src='images/action/Actions-zoom-icon.png' onclick='viewRow(" + cl + ")' />";
					btnActivate = "<img title='Activate' class='imgBtn' style='margin-left:5px;height:20px;width:20px;' src='images/action/Sign-CekList-icon.png' onclick='activateRow(" + cl + ")' />"; 
					jQuery("#tblData").setRowData(ids[i],{act:btnEdit+btnActivate}) 
				} 
			}
		});
		$("#tblData").jqGrid('navGrid','#navTblData',{add:false,edit:false,search:false,del:false,refresh:false});
		
		//add search text
		$("#tblData").jqGrid('addSearchText',["sid","name","address"]);
	});
</script>

<div style="">
	<script>
		$(function(){
			$("#tblData").jqGrid("setGridParam",{postData:{"department_id" :$("#department_id").val()},datatype:"json"}).trigger("reloadGrid");			
			$("#department_id").change(function(){
				var filter = {"is_active":1,"department_id":$("#department_id").val()};
				var data = {obj:"MTahunAjaran",filter: filter,value_field:"id",title_field:"name"};
				renewSelectData(data,"tahun_ajaran_id");
				refreshGrid();				
			});
		});	
	</script>
	<form>
		<div>
			<label class="form mandatory" style="width:100px">Department</label>
			<select id="department_id" name="department_id">
				<?php
					foreach($arrDepartment as $department){
						$selected = $department_id == $department->id ? "selected" : "";
						echo "<option value=$department[id] $selected> $department[name] </option>";
					} 
				?>
			</select>
		</div>
		<div>
			<label class="form" style="width:100px"><?php echo Yii::t("label","Tahun Ajaran");?></label>
			<select id="tahun_ajaran_id" name="tahun_ajaran_id" onChange="refreshGrid()">
				<?php
					foreach($arrTahunAjaran as $tahunAjaran){
						$selected = $tahun_ajaran_id == $tahunAjaran->id ? "selected" : "";
						echo "<option value=$tahunAjaran[id] $selected> $tahunAjaran[name] </option>";
					}
				?>
			</select>
		</div>
	</form>
	<div style="float:rigth">
	<?php 
		$this->widget('application.components.widgets.ActionBar', 
			array("menuAlign" => "right", 
				'items' => array(
					array(
						'id' => "btnRefresh",
						'title' => Yii::t("menu","Segarkan"),
						'icon' => "images/action/Action-Refresh-icon.png",
						'callback'=>"reloadPage"
					),					
					array(
						'id' => "btnPrint",
						'title' => Yii::t("menu","Cetak"),
						'icon' => "images/action/Actions-document-print-icon2.png",
						'callback'=>"printData"
					),					
					
				)
			)
		);
	?>
	</div>	
	<div style="clear:both"></div>	
	<table id="tblData"><tr></tr></table>
	<div id="navTblData"></div>
</div>